package household.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IFlexigridJson {
		
	@SuppressWarnings("unchecked")
	public static List<Map<String, Object>> getRows(Object object){
		Map<String, Object> map = null;
		List<String> cell = null;
		List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
		List<Object> list = (List<Object>)object;
		for (int i = 0; i < list.size(); i++) {	
			cell = new ArrayList<String>();
			map = new HashMap<String, Object>();
			List<String> c = getCellList(cell,list.get(i));
			map.put("id", c.get(0));
			map.put("cell", c);
			rows.add(map);			
		}		
		return rows;
		
	}
	
	private static List<String> getCellList(List<String> list,Object object){
		for (int i = 0; i < object.getClass().getDeclaredFields().length; i++) {
			Field field = object.getClass().getDeclaredFields()[i];
			try {
				field.setAccessible(true);
				list.add(""+field.get(object));				
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}

}
